package com.xiaoqi.api.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoqi.api.mapper.ResourceMapper;
import com.xiaoqi.api.query.ResourceQuery;
import com.xiaoqi.api.service.ResourceService;
import com.xiaoqi.api.utlis.PageResult;
import com.xiaoqi.api.vo.ResourceVO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Resource
    ResourceMapper resourceMapper;

    @Override
    public PageResult<ResourceVO> find(ResourceQuery resourceQuery) {
        System.out.println(resourceQuery);
        //设置是否需要分页
        PageHelper.startPage(resourceQuery.getPage(),resourceQuery.getPageSize());  //告诉PageHelper此处需要分页

        //List
        com.github.pagehelper.Page<ResourceVO> page = (com.github.pagehelper.Page<ResourceVO>) resourceMapper.find(resourceQuery);

        PageInfo<ResourceVO> pageInfo = page.toPageInfo();


        List<ResourceVO> resourceVOList = pageInfo.getList();
        long total = pageInfo.getTotal();

        PageResult<ResourceVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(resourceVOList);

        return pageResult;
    }
}
